package dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.EasyBuyProductCategoryDao;
import entity.Easybuy_product_category;
import utils.PageUtils;

public class EasyBuyProductCategoryDaoImpl extends BaseDao implements EasyBuyProductCategoryDao{

	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	@Override
	public List<Easybuy_product_category> list() {
		// TODO Auto-generated method stub
		List<Easybuy_product_category> catList=new ArrayList<Easybuy_product_category>();
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_product_category";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Easybuy_product_category category=new Easybuy_product_category();
				category.setIconClass(rs.getString("iconClass"));
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				catList.add(category);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return catList;
	}

	@Override
	public PageUtils<Easybuy_product_category> findlist(int currindex, int pagesize) {
		List<Easybuy_product_category> catList=new ArrayList<Easybuy_product_category>();
		PageUtils<Easybuy_product_category> utils=new PageUtils<>();
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_product_category limit ?,?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, currindex);
			pstmt.setInt(2, pagesize);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Easybuy_product_category category=new Easybuy_product_category();
				category.setIconClass(rs.getString("iconClass"));
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				catList.add(category);
			}
			utils.setList(catList);
			int total=0;
			String sqlCount="select count(1) from easybuy_product where isDelete=0";
			
			pstmt=conn.prepareStatement(sqlCount);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				total=rs.getInt(1);
			}
			utils.setPagetotal(total);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return utils;
	}

	@Override
	public List<Easybuy_product_category> findId(int parendId) {
		List<Easybuy_product_category> catList=new ArrayList<Easybuy_product_category>();
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_product_category where parentId=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, parendId);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Easybuy_product_category category=new Easybuy_product_category();
				category.setIconClass(rs.getString("iconClass"));
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				catList.add(category);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return catList;
	}

	@Override
	public int saveOrUpdate(String name,int parentId,int type) {
		int result=0;
		try {
			conn=super.getConnection();
			String sql="INSERT INTO easybuy_product_category(`name`,parentId,type) VALUES(?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, name);
			pstmt.setObject(2, parentId);
			pstmt.setObject(3, type);
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

	@Override
	public Easybuy_product_category finById(int id) {
		Easybuy_product_category category=null;
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_product_category where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				category=new Easybuy_product_category();
				category.setIconClass(rs.getString("iconClass"));
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return category;
	}

	@Override
	public List<Easybuy_product_category> findType(int type) {
		List<Easybuy_product_category> catList=new ArrayList<Easybuy_product_category>();
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_product_category where type=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, type);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Easybuy_product_category category=new Easybuy_product_category();
				category.setIconClass(rs.getString("iconClass"));
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				catList.add(category);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return catList;
	}

	@Override
	public int update(Easybuy_product_category cat) {
		int result=0;
		try {
			conn=super.getConnection();
			String sql="UPDATE easybuy_product_category SET name=?,parentId=?,type=?,iconClass=? WHERE id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, cat.getName());
			pstmt.setObject(2, cat.getParentId());
			pstmt.setObject(3, cat.getType());
			pstmt.setObject(4, cat.getIconClass());
			pstmt.setObject(5, cat.getId());
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

	@Override
	public int deleteProductCategory(int id) {
		int result=0;
		try {
			conn=super.getConnection();
			String sql="delete from easybuy_product_category where id=?";
			
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, id);
			
			result=pstmt.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

	

}
